<template>
  <div>
    <el-form :model="form" ref="form" label-width="120px" :rules="rules">
      <el-form-item label="题目" prop="problem">
        <el-input type="textarea" v-model="form.problem"/>
      </el-form-item>
      <el-form-item label="答案" prop="solution">
        <el-input type="textarea" v-model="form.solution"/>
      </el-form-item>
      <!--TODO 标签下拉框默认从数据库中读取x个，剩下的标签支持远程服务器搜索功能-->
      <el-form-item label="标签" prop="tags">
        <el-select v-model="form.tags"
                   multiple
                   filterable
                   allow-create
                   default-first-option
                   placeholder="请选择题目标签">
          <el-option
              v-for="item in formDefault.tags"
              :key="item"
              :label="item"
              :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="difficulty">
        <el-select v-model="form.difficulty" placeholder="请选择">
          <el-option
              v-for="item in formDefault.difficulty"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark"/>
      </el-form-item>
      <el-form-item>
        <el-button type="success" size="small" @click="submit">提交</el-button>
        <el-button type="danger" size="small" @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, post } from '@/plugins/axios'
import {Message} from "element-ui"

export default {
  name: "edit",
  props: ['id', 'bankId'],
  created() {
    if (this.id != null) {
      this.getOne()
    }
    this.getTags()
  },
  data() {
    const module = '/question'
    return {
      uri: {
        getOne: module + '/selectOne/',
        saveOrUpdate: module + '/saveOrUpdate',
        getTags: '/questionTags/selectAll'
      },
      form: {
        bankId: this.bankId,
        problem: '',
        solution: '',
        tags: [],
        difficulty: '',
        remark: ''
      },
      formDefault: {
        tags: [],
        difficulty: [
          {label: '未知', value: 0},
          {label: '简单', value: 1},
          {label: '中等', value: 2},
          {label: '困难', value: 3}
        ]
      },
      rules: {
        problem: [
          {required: true, message: '请输入题目', trigger: 'blur'}
        ],
        solution: [
          {required: true, message: '请输入答案', trigger: 'blur'}
        ],
        tags: [
          {required: true, message: '请选择至少一个标签', trigger: 'blur'}
        ],
        difficulty: [
          {required: true, message: '请选择题目难度', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          post(this.uri.saveOrUpdate, this.form, response => {
            if (response.code === 200) {
              Message.success(response.message)
            }
            this.$emit('update:editDialogShow',false)
            this.$emit('getQuestionData')
          })
        } else {
          return false
        }
      });
    },
    cancel() {
      this.$emit('update:editDialogShow',false)
    },
    getOne() {
      get(this.uri.getOne + this.id, {}, response => {
        for (const key in this.form) {
          this.form[key] = response.data[key]
        }
        this.form.tags = this.form.tags.split(',')
        this.form.id = response.data.id
      })
    },
    getTags() {
      get(this.uri.getTags, {status: 1}, response => {
        const tags = response.data;
        for (const item in tags) {
          this.formDefault.tags.push(tags[item].tag)
        }
      })
    }
  }
}
</script>

<style scoped lang="less">

</style>